<?php
/**
 * 유틸리티 - Cookie
 *
 * 프레임워크내에서 쿠키를 구울때 사용하는 util
 *
 * <code>
 * <?php
 * $utilCookie = new utilCookie();
 * //쿠키 등록
 * $utilCookie->setCookie('foo', 'bar', 3600, true);
 *
 * echo $_COOKIE['foo'];
 *
 * //모든 쿠키값을 해제함
 * $utilCookie->destroy();
 * print_r($_COOKIE);
 *
 * ?>
 * </code>
 *
 * @package util
 * @author  ul jung
 * @version 1.0
 */
class utilCookie
{
    private $sDomain;

    /**
     * utilCookie Construct
     *
     * @param    String    $sHost    쿠키 도메인, 값을 지정하지 않을경우 서버 기본 호스트 참조
     */
    public function __construct($sHost = null)
    {
        $this->sDomain = $this->setDomain($sHost);
    }


    /**
     * 쿠키 도메인 설정
     *
     * @param    String    $sHost    쿠키 도메인, 값을 지정하지 않을경우 서버 기본 호스트 참조
     *
     * @return     String    쿠기 도메인
     */
    public function setDomain($sHost=null)
    {
        $sDomain = $sHost ? $sHost : $_SERVER['HTTP_HOST'];

        return $this->removePort($this->addDotPrefix($this->replaceWorldWideWeb($sDomain)));
    }


    /**
     * 쿠키 설정
     *
     * <code>
     * <?php
     * $utilCookie = new utilCookie();
     * $utilCookie->setCookie('foo', 'bar', 3600, true);
     *
     * echo $_COOKIE['foo'];
     * ?>
     * </code>
     *
     * @param string    $sKey           COOKIE Key
     * @param string    $sVal           COOKIE VALUE
     * @param int       $iTime          COOKIE expire time
     * @param bool      $bOnlyPhp       When TRUE the cookie will be made accessible only through the HTTP protocol. (Added in PHP 5.2.0)
     * @return bool     true or false
     */
    public function setCookie($sKey, $sVal, $iTime=0, $bOnlyPhp=false)
    {
        return setcookie($sKey,  $sVal, $iTime, "/", $this->sDomain, 0, $bOnlyPhp);
    }


    /**
     * 쿠키값 초기화
     *
     * 해당도메인에서 사용하던 모든 쿠키값을 초기화합니다.
     *
     * <code>
     * <?php
     * $utilCookie = new utilCookie();
     * $utilCookie->destory();
     * print_r($_COOKIE);
     *
     * //result
     * array();
     * ?>
     * </code>
     *
     * @return    Boolean    true
     */
    public function destroy()
    {
        foreach ($_COOKIE as $sKey => $sVal) {
            $this->setCookie($sKey, '', time()-3600);
        }
        return true;
    }



    /**
     * replace www string
     *
     * @param string $sDomain
     *
     * @return  string $sDomain
     */
    private function replaceWorldWideWeb($sDomain)
    {
        if (strtolower(substr($sDomain, 0, 4)) == 'www.' ) {

            $sDomain = substr($sDomain, 4);
        }
        return $sDomain;
    }


    /**
     * Add the dot prefix to ensure compatibility with subdomains
     *
     * @param   string $sDomain
     *
     * @return  string $sDomain
     */
    private function addDotPrefix($sDomain)
    {
        if (!is_numeric(substr($sDomain, 0, 2)) && substr($sDomain, 0, 1) != '.') {
            $sDomain = '.'.$sDomain;
        }
        return $sDomain;
    }


    /**
     * Remove port information.
     *
     * @param string    $sDomain
     *
     * @return string   $sDomain;
     */
    private function removePort($sDomain)
    {
        $mPort = strpos($sDomain, ':');
        if ($mPort !== false) {
            $sDomain = substr($sDomain, 0, $mPort);
        }
        return $sDomain;
    }
}
